package com.beidouin.sensortest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.HardWare.PCF8591;
import com.friendlyarm.AndroidSDK.FileCtlEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;

import static com.HardWare.PCF8591.ADC_Init;

public class AdChannelActivity extends AppCompatActivity implements Runnable,CheckBox.OnCheckedChangeListener{


    TextView[] adChannel=new TextView[4];
    TextView[] adtxv=new TextView[4];
    CheckBox[] adChoose=new CheckBox[4];
    boolean[] adChannelIsChecked=new boolean[4];
    int devFD=-1;
    Handler handler;
    Thread t = new Thread(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_channel);

        adChannel[0]= (TextView) findViewById(R.id.ad_channel1);
        adChannel[1]= (TextView) findViewById(R.id.ad_channel2);
        adChannel[2]= (TextView) findViewById(R.id.ad_channel3);
        adChannel[3]= (TextView) findViewById(R.id.ad_channel4);
        adtxv[0]= (TextView) findViewById(R.id.adtxv1);
        adtxv[1]= (TextView) findViewById(R.id.adtxv2);
        adtxv[2]= (TextView) findViewById(R.id.adtxv3);
        adtxv[3]= (TextView) findViewById(R.id.adtxv4);

        adChoose[0]= (CheckBox) findViewById(R.id.channel_ck1);
        adChoose[1]= (CheckBox) findViewById(R.id.channel_ck2);
        adChoose[2]= (CheckBox) findViewById(R.id.channel_ck3);
        adChoose[3]= (CheckBox) findViewById(R.id.channel_ck4);

        for(int i=0;i<4;i++)
        {
            adChoose[i].setOnCheckedChangeListener(this);
            adChannelIsChecked[i]=false;
            adtxv[i].setVisibility(View.GONE);
            adChannel[i].setVisibility(View.GONE);
        }


        devFD = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
        ADC_Init(devFD);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x101) {
                    adChannel[0].setText(" "+msg.getData().getInt("ad0"));
                    adChannel[1].setText(" "+msg.getData().getInt("ad1"));
                    adChannel[2].setText(" "+msg.getData().getInt("ad2"));
                    adChannel[3].setText(" "+msg.getData().getInt("ad3"));
                }
                super.handleMessage(msg);
            }
        };
        t.start();
    }


    @Override
    protected void onDestroy() {
        //将线程销毁掉
        HardwareControler.close(devFD);
        t.interrupt();
        super.onDestroy();
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted())
        {
            Message m=handler.obtainMessage();
            Bundle bundle =new Bundle();
            m.what=0x101;
            int[] ad=new int[4];
            for(int i=0;i<4;i++) {
                ad[i] = PCF8591.readADC_bychannel(devFD, i);
            }
            bundle.putInt("ad0", ad[0]);
            bundle.putInt("ad1", ad[1]);
            bundle.putInt("ad2", ad[2]);
            bundle.putInt("ad3", ad[3]);
            m.setData(bundle);
            handler.sendMessage(m);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked)
        {
            switch (buttonView.getId())
            {
                case R.id.channel_ck1:
                {
                    adtxv[0].setVisibility(View.VISIBLE);
                    adChannel[0].setVisibility(View.VISIBLE);
                }
                break;
                case R.id.channel_ck2:
                {
                    adtxv[1].setVisibility(View.VISIBLE);
                    adChannel[1].setVisibility(View.VISIBLE);
                }
                break;
                case R.id.channel_ck3:
                {
                    adtxv[2].setVisibility(View.VISIBLE);
                    adChannel[2].setVisibility(View.VISIBLE);
                }
                break;
                case R.id.channel_ck4:
                {
                    adtxv[3].setVisibility(View.VISIBLE);
                    adChannel[3].setVisibility(View.VISIBLE);
                }
                break;
            }
        }
        else
        {
            switch (buttonView.getId())
            {
                case R.id.channel_ck1:
                {
                    adtxv[0].setVisibility(View.GONE);
                    adChannel[0].setVisibility(View.GONE);
                }
                break;
                case R.id.channel_ck2:
                {
                    adtxv[1].setVisibility(View.GONE);
                    adChannel[1].setVisibility(View.GONE);
                }
                break;
                case R.id.channel_ck3:
                {
                    adtxv[2].setVisibility(View.GONE);
                    adChannel[2].setVisibility(View.GONE);
                }
                break;
                case R.id.channel_ck4:
                {
                    adtxv[3].setVisibility(View.GONE);
                    adChannel[3].setVisibility(View.GONE);
                }
                break;
            }
        }
    }
}
